using OSGeo.MapGuide;
using System;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.UI;
using System.Xml;

public class LayerDefinitionFactory
{
    public MgByteReader layerDefContent = null;
    public XmlDataDocument LayerDefXml = null;
    public string LayerDefXmlString = "";

    public bool AddExtendedData(string FClassProperty, string AttClassProperty, string AttributeClass, string ResourceId, string FName, string RelateType, string ForceOneToOne, string ExtName, string FeatureClass)
    {
        try
        {
            XmlNode pFeature = this.LayerDefXml.GetElementsByTagName("FeatureSource")[0];
            XmlElement pExtension = this.LayerDefXml.CreateElement("Extension");
            XmlElement pAttributeRelate = this.LayerDefXml.CreateElement("AttributeRelate");
            XmlElement pRelateProperty = this.LayerDefXml.CreateElement("RelateProperty");
            XmlElement pElmt = this.LayerDefXml.CreateElement("FeatureClassProperty");
            pElmt.InnerText = FClassProperty;
            pRelateProperty.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("AttributeClassProperty");
            pElmt.InnerText = AttClassProperty;
            pRelateProperty.AppendChild(pElmt);
            pAttributeRelate.AppendChild(pRelateProperty);
            pElmt = this.LayerDefXml.CreateElement("AttributeClass");
            pElmt.InnerText = AttributeClass;
            pAttributeRelate.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("ResourceId");
            pElmt.InnerText = ResourceId;
            pAttributeRelate.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("Name");
            pElmt.InnerText = FName;
            pAttributeRelate.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("AttributeNameDelimiter");
            pAttributeRelate.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("RelateType");
            pElmt.InnerText = RelateType;
            pAttributeRelate.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("ForceOneToOne");
            pElmt.InnerText = ForceOneToOne;
            pAttributeRelate.AppendChild(pElmt);
            pExtension.AppendChild(pAttributeRelate);
            pElmt = this.LayerDefXml.CreateElement("Name");
            pElmt.InnerText = ExtName;
            pExtension.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("FeatureClass");
            pElmt.InnerText = FeatureClass;
            pExtension.AppendChild(pElmt);
            pFeature.AppendChild(pExtension);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddLineLayerRule(Page oPage, string sFilter, string width, string linestyle, string color)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("LineRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("LineSymbolization2D/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("LineSymbolization2D/Thickness").InnerText = width;
            pRule.SelectSingleNode("LineSymbolization2D/Color").InnerText = color.ToUpper();
            this.LayerDefXml.GetElementsByTagName("LineTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddLineLayerRule(Page oPage, string sFilter, string width, string linestyle, string color, bool bDelete)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("LineRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("LineRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("LineSymbolization2D/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("LineSymbolization2D/Thickness").InnerText = width;
            pRule.SelectSingleNode("LineSymbolization2D/Color").InnerText = color.ToUpper();
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("LineTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("LineTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddMarkLayerRule(string sFilter, string width, string height, string color)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeX").InnerText = width;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeY").InnerText = height;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/ForegroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/BackgroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Edge/Color").InnerText = color.ToUpper();
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddMarkLayerRule(Page oPage, string sFilter, string width, string height, string color)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeX").InnerText = width;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeY").InnerText = height;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/ForegroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/BackgroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Edge/Color").InnerText = color.ToUpper();
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddMarkLayerRule2(string sFilter, string width, string height, string color)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeX").InnerText = width;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeY").InnerText = height;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Edge/Color").InnerText = color.ToUpper();
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddPolygonLayerRule(string sFilter, string width, string linestyle, string LineColor, string FillType, string FillColor)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("AreaRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Thickness").InnerText = width;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Color").InnerText = LineColor.ToUpper();
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/FillPattern").InnerText = FillType;
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/ForegroundColor").InnerText = FillColor.ToUpper();
            this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddPolygonLayerRule(Page oPage, string sFilter, string width, string linestyle, string LineColor, string FillType, string FillColor)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("AreaRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Thickness").InnerText = width;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Color").InnerText = LineColor.ToUpper();
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/FillPattern").InnerText = FillType;
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/ForegroundColor").InnerText = FillColor.ToUpper();
            this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddPolygonLayerRule(Page oPage, string sFilter, string width, string linestyle, string LineColor, string FillType, string FillColor, bool bDelete)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("AreaRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("AreaRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Thickness").InnerText = width;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Color").InnerText = LineColor.ToUpper();
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/FillPattern").InnerText = FillType;
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/ForegroundColor").InnerText = FillColor.ToUpper();
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public void AddPropertyMap(string sname)
    {
        XmlElement pElmt = this.LayerDefXml.CreateElement("PropertyMapping");
        XmlElement pElmtAdd = this.LayerDefXml.CreateElement("Name");
        pElmtAdd.InnerText = sname;
        pElmt.AppendChild(pElmtAdd);
        pElmtAdd = this.LayerDefXml.CreateElement("Value");
        pElmtAdd.InnerText = sname;
        pElmt.AppendChild(pElmtAdd);
        this.LayerDefXml.GetElementsByTagName("VectorLayerDefinition")[0].InsertAfter(pElmt, this.LayerDefXml.GetElementsByTagName("FeatureNameType")[0]);
    }

    public bool AddRedlineLineLayerRule(string sFilter, string width, string linestyle, string color)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("LineRule")[0];
            if (pRule.SelectSingleNode("Filter").InnerText != "")
            {
                pRule = this.LayerDefXml.GetElementsByTagName("LineRule")[0].Clone();
            }
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("LineSymbolization2D/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("LineSymbolization2D/Thickness").InnerText = width;
            pRule.SelectSingleNode("LineSymbolization2D/Color").InnerText = color.ToUpper();
            this.LayerDefXml.GetElementsByTagName("LineTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddRedlineMarkLayerRule(string sFilter, string width, string height, string color)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            if (pRule.SelectSingleNode("Filter").InnerText != "")
            {
                pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            }
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeX").InnerText = width;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeY").InnerText = height;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/ForegroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/BackgroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Edge/Color").InnerText = color.ToUpper();
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddRedlinePolygonLayerRule(string sFilter, string width, string linestyle, string LineColor, string FillType, string FillColor)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("AreaRule")[0];
            if (pRule.SelectSingleNode("Filter").InnerText != "")
            {
                pRule = this.LayerDefXml.GetElementsByTagName("AreaRule")[0].Clone();
            }
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Thickness").InnerText = width;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Color").InnerText = LineColor.ToUpper();
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/FillPattern").InnerText = FillType;
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/ForegroundColor").InnerText = FillColor.ToUpper();
            this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddRedlineTextLayerRule(string sFilter, string width, string color, string sFont, string sFill, string textB, string textI, string textU)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            if (pRule.SelectSingleNode("Filter").InnerText != "")
            {
                pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            }
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("Label/SizeY").InnerText = width;
            pRule.SelectSingleNode("Label/FontName").InnerText = sFont;
            pRule.SelectSingleNode("Label/ForegroundColor").InnerText = color.ToUpper();
            if (sFill != "")
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Opaque";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = sFill;
            }
            else
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Transparent";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = "ffffffff";
            }
            pRule.SelectSingleNode("Label/Bold").InnerText = textB;
            pRule.SelectSingleNode("Label/Italic").InnerText = textI;
            pRule.SelectSingleNode("Label/Underlined").InnerText = textU;
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddSignSymbolLayerRule(string symbol, string sRot, string symDesc)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("PointSymbolization2D/W2D/W2DSymbol/LibraryItemName").InnerText = symbol;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/Rotation").InnerText = sRot;
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddSymbolLayerRule(string sFilter, string width, string height, string color, string symbol, string Symbolres)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/W2DSymbol/LibraryItemName").InnerText = symbol;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/W2DSymbol/ResourceId").InnerText = Symbolres;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/SizeX").InnerText = width;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/SizeY").InnerText = height;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/FillColor").InnerText = color.ToUpper();
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddTextLayerRule(string sFilter, string width, string color, string sFont, string sFill, string textB, string textI, string textU, string stext)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("Label/SizeY").InnerText = width;
            pRule.SelectSingleNode("Label/Text").InnerText = stext;
            pRule.SelectSingleNode("Label/FontName").InnerText = sFont;
            pRule.SelectSingleNode("Label/ForegroundColor").InnerText = color.ToUpper();
            if (sFill != "")
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Opaque";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = sFill;
            }
            else
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Transparent";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = "ffffffff";
            }
            pRule.SelectSingleNode("Label/Bold").InnerText = textB;
            pRule.SelectSingleNode("Label/Italic").InnerText = textI;
            pRule.SelectSingleNode("Label/Underlined").InnerText = textU;
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddTextLayerRule(Page oPage, string sFilter, string width, string color, string sFont, string sFill, string textB, string textI, string textU)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("Label/SizeY").InnerText = width;
            pRule.SelectSingleNode("Label/FontName").InnerText = sFont;
            pRule.SelectSingleNode("Label/ForegroundColor").InnerText = color.ToUpper();
            if (sFill != "")
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Opaque";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = sFill;
            }
            else
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Transparent";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = "ffffffff";
            }
            pRule.SelectSingleNode("Label/Bold").InnerText = textB;
            pRule.SelectSingleNode("Label/Italic").InnerText = textI;
            pRule.SelectSingleNode("Label/Underlined").InnerText = textU;
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddTextLayerRule(string sFilter, string width, string color, string sFont, string sFill, string textB, string textI, string textU, string stext, string rotation, string align)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("Label/SizeY").InnerText = width;
            pRule.SelectSingleNode("Label/Text").InnerText = stext;
            pRule.SelectSingleNode("Label/FontName").InnerText = sFont;
            pRule.SelectSingleNode("Label/ForegroundColor").InnerText = color.ToUpper();
            if (sFill != "")
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Opaque";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = sFill;
            }
            else
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Transparent";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = "ffffffff";
            }
            if (rotation != "")
            {
                pRule.SelectSingleNode("Label/Rotation").InnerText = rotation;
            }
            if (align != "")
            {
                pRule.SelectSingleNode("Label/HorizontalAlignment").InnerText = "'" + align + "'";
            }
            pRule.SelectSingleNode("Label/Bold").InnerText = textB;
            pRule.SelectSingleNode("Label/Italic").InnerText = textI;
            pRule.SelectSingleNode("Label/Underlined").InnerText = textU;
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool AddTextLayerRule(Page oPage, string sFilter, string width, string color, string sFont, string sFill, string textB, string textI, string textU, string stext, bool bDelete)
    {
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("Label/SizeY").InnerText = width;
            pRule.SelectSingleNode("Label/Text").InnerText = "'" + stext + "'";
            pRule.SelectSingleNode("Label/FontName").InnerText = sFont;
            pRule.SelectSingleNode("Label/ForegroundColor").InnerText = color.ToUpper();
            if (sFill != "")
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Opaque";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = sFill;
            }
            else
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Transparent";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = "ffffffff";
            }
            pRule.SelectSingleNode("Label/Bold").InnerText = textB;
            pRule.SelectSingleNode("Label/Italic").InnerText = textI;
            pRule.SelectSingleNode("Label/Underlined").InnerText = textU;
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public string ChangeNewRasterLocation(string sXml, string newName)
    {
        try
        {
            this.LayerDefXml = new XmlDataDocument();
            this.LayerDefXml.PreserveWhitespace = true;
            this.LayerDefXml.LoadXml(sXml);
            string oldName = this.LayerDefXml.SelectSingleNode("FeatureSource/Parameter/Value").InnerText.Split(new char[] { '/' })[1];
            string vv = sXml.Replace(oldName, newName);
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return vv;
        }
        catch
        {
            return "";
        }
    }

    public bool CreateCognosPolygonLayerRule(string Filter, string LegendLabel, string LabelText, string FillColor, string BackgroundStyle, string BackgroundColor, string FontName, string SizeY, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0];
            }
            XmlElement pAreaTypeStyle = this.LayerDefXml.CreateElement("AreaTypeStyle");
            XmlNode pRule = this.LayerDefXml.CreateElement("AreaRule");
            XmlElement pElmt = this.LayerDefXml.CreateElement("LegendLabel");
            pElmt.InnerText = this.ReplaceHebrewForXml(LegendLabel);
            pRule.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("Filter");
            pElmt.InnerText = Filter;
            pRule.AppendChild(pElmt);
            if (LabelText != "")
            {
                XmlElement pLabel = this.LayerDefXml.CreateElement("Label");
                pElmt = this.LayerDefXml.CreateElement("Unit");
                pElmt.InnerText = "Points";
                pLabel.AppendChild(pElmt);
                pElmt = this.LayerDefXml.CreateElement("SizeContext");
                pElmt.InnerText = "DeviceUnits";
                pLabel.AppendChild(pElmt);
                pElmt = this.LayerDefXml.CreateElement("SizeX");
                pElmt.InnerText = "0.5";
                pLabel.AppendChild(pElmt);
                pElmt = this.LayerDefXml.CreateElement("SizeY");
                pElmt.InnerText = SizeY;
                pLabel.AppendChild(pElmt);
                pElmt = this.LayerDefXml.CreateElement("Text");
                pElmt.InnerText = "COGNOS_DATALABEL";
                pLabel.AppendChild(pElmt);
                pElmt = this.LayerDefXml.CreateElement("FontName");
                pElmt.InnerText = FontName;
                pLabel.AppendChild(pElmt);
                pElmt = this.LayerDefXml.CreateElement("ForegroundColor");
                pElmt.InnerText = "ff000000";
                pLabel.AppendChild(pElmt);
                pElmt = this.LayerDefXml.CreateElement("BackgroundColor");
                pElmt.InnerText = BackgroundColor;
                pLabel.AppendChild(pElmt);
                pElmt = this.LayerDefXml.CreateElement("BackgroundStyle");
                pElmt.InnerText = BackgroundStyle;
                pLabel.AppendChild(pElmt);
                pElmt = this.LayerDefXml.CreateElement("VerticalAlignment");
                pElmt.InnerText = "'Halfline'";
                pLabel.AppendChild(pElmt);
                XmlElement pAdvanced = this.LayerDefXml.CreateElement("AdvancedPlacement");
                pElmt = this.LayerDefXml.CreateElement("ScaleLimit");
                pElmt.InnerText = "0.5";
                pAdvanced.AppendChild(pElmt);
                pLabel.AppendChild(pAdvanced);
                pRule.AppendChild(pLabel);
            }
            XmlElement pSymbol = this.LayerDefXml.CreateElement("AreaSymbolization2D");
            XmlElement pFill = this.LayerDefXml.CreateElement("Fill");
            XmlElement pStroke = this.LayerDefXml.CreateElement("Stroke");
            pElmt = this.LayerDefXml.CreateElement("FillPattern");
            pElmt.InnerText = "Solid";
            pFill.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("ForegroundColor");
            pElmt.InnerText = FillColor;
            pFill.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("BackgroundColor");
            pElmt.InnerText = "FF000000";
            pFill.AppendChild(pElmt);
            pSymbol.AppendChild(pFill);
            pElmt = this.LayerDefXml.CreateElement("LineStyle");
            pElmt.InnerText = "Solid";
            pStroke.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("Thickness");
            pElmt.InnerText = "0";
            pStroke.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("Color");
            pElmt.InnerText = "FF000000";
            pStroke.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("Unit");
            pElmt.InnerText = "Centimeters";
            pStroke.AppendChild(pElmt);
            pElmt = this.LayerDefXml.CreateElement("SizeContext");
            pElmt.InnerText = "DeviceUnits";
            pStroke.AppendChild(pElmt);
            pSymbol.AppendChild(pStroke);
            pRule.AppendChild(pSymbol);
            if (bDelete && (pRule4Del != null))
            {
                pAreaTypeStyle.AppendChild(pRule);
                this.LayerDefXml.GetElementsByTagName("VectorScaleRange")[0].RemoveChild(pRule4Del);
                this.LayerDefXml.GetElementsByTagName("VectorScaleRange")[0].AppendChild(pAreaTypeStyle);
            }
            else
            {
                this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].AppendChild(pRule);
            }
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateFireCarsLayerDef(string name, string datasource, string featurename, string color)
    {
        try
        {
            this.LayerDefXml = new XmlDataDocument();
            this.LayerDefXml.Load(ConfigurationManager.AppSettings["PHYSICALPATH"] + @"\App_Data\" + name + ".xml");
            this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            XmlNodeList lst = this.LayerDefXml.GetElementsByTagName("ForegroundColor");
            for (int i = 1; i < lst.Count; i++)
            {
                lst[i].InnerText = color;
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateLayerDef(string sContent)
    {
        try
        {
            this.LayerDefXml = new XmlDataDocument();
            this.LayerDefXml.PreserveWhitespace = true;
            this.LayerDefXml.LoadXml(sContent);
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateLayerDef(string name, string datasource, string featurename, string tooltip)
    {
        try
        {
            this.LayerDefXml = new XmlDataDocument();
            this.LayerDefXml.Load(ConfigurationManager.AppSettings["PHYSICALPATH"] + @"\App_Data\" + name + ".xml");
            this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            if ((this.LayerDefXml.GetElementsByTagName("ToolTip").Count > 0) && (tooltip != ""))
            {
                this.LayerDefXml.GetElementsByTagName("ToolTip")[0].InnerText = tooltip;
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateLayerDef(string name, string datasource, string featurename, string geomName, string tooltip)
    {
        try
        {
            this.LayerDefXml = new XmlDataDocument();
            this.LayerDefXml.Load(ConfigurationManager.AppSettings["PHYSICALPATH"] + @"\App_Data\" + name + ".xml");
            this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            this.LayerDefXml.GetElementsByTagName("Geometry")[0].InnerText = geomName;
            if ((this.LayerDefXml.GetElementsByTagName("ToolTip").Count > 0) && (tooltip != ""))
            {
                this.LayerDefXml.GetElementsByTagName("ToolTip")[0].InnerText = tooltip;
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateLayerDef(string name, Page oPage, string datasource, string featurename, string tooltip)
    {
        try
        {
            this.LayerDefXml = new XmlDataDocument();
            this.LayerDefXml.Load(oPage.Request.ServerVariables["APPL_PHYSICAL_PATH"] + @"App_Data\" + name + ".xml");
            this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            if ((this.LayerDefXml.GetElementsByTagName("ToolTip").Count > 0) && (tooltip != ""))
            {
                this.LayerDefXml.GetElementsByTagName("ToolTip")[0].InnerText = tooltip;
            }
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateLineLayerRule(Page oPage, string sFilter, string width, string linestyle, string color, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("LineRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("LineRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("LineSymbolization2D/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("LineSymbolization2D/Thickness").InnerText = width;
            pRule.SelectSingleNode("LineSymbolization2D/Color").InnerText = color.ToUpper();
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("LineTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("LineTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateMarkLayerRule(string sFilter, string width, string height, string color, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeX").InnerText = width;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeY").InnerText = height;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/ForegroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/BackgroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Edge/Color").InnerText = color.ToUpper();
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateMarkLayerRule(Page oPage, string sFilter, string width, string height, string color, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeX").InnerText = width;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeY").InnerText = height;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/ForegroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Fill/BackgroundColor").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Edge/Color").InnerText = color.ToUpper();
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateMarkLayerRule2(string sFilter, string width, string height, string color, string thickness, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeX").InnerText = width;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/SizeY").InnerText = height;
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Edge/Color").InnerText = color.ToUpper();
            pRule.SelectSingleNode("PointSymbolization2D/Mark/Edge/Thickness").InnerText = thickness;
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreatePolygonLayerRule(string sFilter, string width, string linestyle, string LineColor, string FillType, string FillColor, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("AreaRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("AreaRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Thickness").InnerText = width;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Color").InnerText = LineColor.ToUpper();
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/FillPattern").InnerText = FillType;
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/ForegroundColor").InnerText = FillColor.ToUpper();
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreatePolygonLayerRule(Page oPage, string sFilter, string width, string linestyle, string LineColor, string FillType, string FillColor, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("AreaRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("AreaRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/LineStyle").InnerText = linestyle;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Thickness").InnerText = width;
            pRule.SelectSingleNode("AreaSymbolization2D/Stroke/Color").InnerText = LineColor.ToUpper();
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/FillPattern").InnerText = FillType;
            pRule.SelectSingleNode("AreaSymbolization2D/Fill/ForegroundColor").InnerText = FillColor.ToUpper();
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateSignSymbolLayerDef(string xmlName, string datasource, string featurename)
    {
        try
        {
            this.LayerDefXml = new XmlDataDocument();
            this.LayerDefXml.Load(ConfigurationManager.AppSettings["PHYSICALPATH"] + @"\App_Data\" + xmlName + ".xml");
            this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateSignSymbolLayerRule(string symName, string sRot, string symDesc, bool bDelete, int i)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("LegendLabel").InnerText = this.ReplaceHebrewForXml(symDesc);
            pRule.SelectSingleNode("PointSymbolization2D/W2D/W2DSymbol/LibraryItemName").InnerText = symName;
            pRule.SelectSingleNode("Filter").InnerText = "KEY=" + ((i + 1)).ToString();
            pRule.SelectSingleNode("PointSymbolization2D/W2D/Rotation").InnerText = sRot;
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateSymbolLayerRule(string sFilter, string width, string height, string color, string symbol, string Symbolres, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/W2DSymbol/LibraryItemName").InnerText = symbol;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/W2DSymbol/ResourceId").InnerText = Symbolres;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/SizeX").InnerText = width;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/SizeY").InnerText = height;
            pRule.SelectSingleNode("PointSymbolization2D/W2D/FillColor").InnerText = color.ToUpper();
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateTextLayerRule(string sFilter, string width, string color, string sFont, string sFill, string textB, string textI, string textU, string stext, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("Label/SizeY").InnerText = width;
            pRule.SelectSingleNode("Label/Text").InnerText = stext;
            pRule.SelectSingleNode("Label/FontName").InnerText = sFont;
            pRule.SelectSingleNode("Label/ForegroundColor").InnerText = color.ToUpper();
            if (sFill != "")
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Opaque";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = sFill;
            }
            else
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Transparent";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = "ffffffff";
            }
            pRule.SelectSingleNode("Label/Bold").InnerText = textB;
            pRule.SelectSingleNode("Label/Italic").InnerText = textI;
            pRule.SelectSingleNode("Label/Underlined").InnerText = textU;
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateTextLayerRule(Page oPage, string sFilter, string width, string color, string sFont, string sFill, string textB, string textI, string textU, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("Label/SizeY").InnerText = width;
            pRule.SelectSingleNode("Label/FontName").InnerText = sFont;
            pRule.SelectSingleNode("Label/ForegroundColor").InnerText = color.ToUpper();
            if (sFill != "")
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Opaque";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = sFill;
            }
            pRule.SelectSingleNode("Label/Bold").InnerText = textB;
            pRule.SelectSingleNode("Label/Italic").InnerText = textI;
            pRule.SelectSingleNode("Label/Underlined").InnerText = textU;
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateTextLayerRule(Page oPage, string sFilter, string width, string color, string sFont, string sFill, string textB, string textI, string textU, string stext, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("Label/SizeY").InnerText = width;
            pRule.SelectSingleNode("Label/Text").InnerText = "'" + stext + "'";
            pRule.SelectSingleNode("Label/FontName").InnerText = sFont;
            pRule.SelectSingleNode("Label/ForegroundColor").InnerText = color.ToUpper();
            if (sFill != "")
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Opaque";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = sFill;
            }
            pRule.SelectSingleNode("Label/Bold").InnerText = textB;
            pRule.SelectSingleNode("Label/Italic").InnerText = textI;
            pRule.SelectSingleNode("Label/Underlined").InnerText = textU;
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool CreateTextLayerRule(string sFilter, string width, string color, string sFont, string sFill, string textB, string textI, string textU, string stext, string rotation, string align, bool bDelete)
    {
        try
        {
            XmlNode pRule4Del = null;
            if (bDelete)
            {
                pRule4Del = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
            }
            XmlNode pRule = this.LayerDefXml.GetElementsByTagName("PointRule")[0].Clone();
            pRule.SelectSingleNode("Filter").InnerText = sFilter;
            pRule.SelectSingleNode("Label/SizeY").InnerText = width;
            pRule.SelectSingleNode("Label/Text").InnerText = stext;
            pRule.SelectSingleNode("Label/FontName").InnerText = sFont;
            pRule.SelectSingleNode("Label/ForegroundColor").InnerText = color.ToUpper();
            if (sFill != "")
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Opaque";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = sFill;
            }
            else
            {
                pRule.SelectSingleNode("Label/BackgroundStyle").InnerText = "Transparent";
                pRule.SelectSingleNode("Label/BackgroundColor").InnerText = "ffffffff";
            }
            if (rotation != "")
            {
                pRule.SelectSingleNode("Label/Rotation").InnerText = rotation;
            }
            if (align != "")
            {
                pRule.SelectSingleNode("Label/HorizontalAlignment").InnerText = "'" + align + "'";
            }
            pRule.SelectSingleNode("Label/Bold").InnerText = textB;
            pRule.SelectSingleNode("Label/Italic").InnerText = textI;
            pRule.SelectSingleNode("Label/Underlined").InnerText = textU;
            if (bDelete)
            {
                this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(pRule4Del);
            }
            this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].AppendChild(pRule);
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public string GetLayerFeatureProvider(string sFeatureXml, out string DataSourceName)
    {
        DataSourceName = "";
        try
        {
            XmlDocument oXml = new XmlDocument();
            oXml.PreserveWhitespace = true;
            oXml.LoadXml(sFeatureXml);
            string Provider = oXml.SelectSingleNode("FeatureSource/Provider").InnerText;
            foreach (XmlElement ParameterItem in oXml.SelectNodes("FeatureSource/Parameter"))
            {
                if (ParameterItem.SelectSingleNode("Name").InnerText == "ConnectionString")
                {
                    string[] arrVal = ParameterItem.SelectSingleNode("Value").InnerText.Split(new char[] { ';' });
                    for (int i = 0; i < arrVal.Length; i++)
                    {
                        if (arrVal[i].IndexOf("Database") != -1)
                        {
                            DataSourceName = arrVal[i].Split(new char[] { '=' })[1];
                            break;
                        }
                    }
                }
                else if (ParameterItem.SelectSingleNode("Name").InnerText == "DataStore")
                {
                    DataSourceName = ParameterItem.SelectSingleNode("Value").InnerText;
                    break;
                }
            }
            return Provider.Split(new char[] { '.' })[1];
        }
        catch
        {
            return "";
        }
    }

    public string GetLayerFilter(string sLayDef)
    {
        try
        {
            this.LayerDefXml = new XmlDataDocument();
            this.LayerDefXml.PreserveWhitespace = true;
            this.LayerDefXml.LoadXml(sLayDef);
            XmlNodeList pnlst = this.LayerDefXml.GetElementsByTagName("Filter");
            if (pnlst != null)
            {
                return pnlst.Item(0).InnerText.Replace("\"", "");
            }
            return "";
        }
        catch
        {
            return "";
        }
    }

    public string[] GetMinMaxScale(string sLayerXml)
    {
        string[] res = new string[2];
        double MinScale = 1000000000.0;
        double MaxScale = 0.0;
        double StartMinScale = MinScale;
        double StartMaxScale = MaxScale;
        try
        {
            XmlDocument oXml = new XmlDocument();
            oXml.LoadXml(sLayerXml);
            XmlNodeList lst = oXml.GetElementsByTagName("VectorScaleRange");
            for (int i = 0; i < lst.Count; i++)
            {
                if ((lst[i].SelectSingleNode("MaxScale") != null) && (double.Parse(lst[i].SelectSingleNode("MaxScale").InnerText) > MaxScale))
                {
                    MaxScale = double.Parse(lst[i].SelectSingleNode("MaxScale").InnerText);
                }
                if ((lst[i].SelectSingleNode("MinScale") != null) && (double.Parse(lst[i].SelectSingleNode("MinScale").InnerText) < MinScale))
                {
                    MinScale = double.Parse(lst[i].SelectSingleNode("MinScale").InnerText);
                }
            }
            if (MinScale == StartMinScale)
            {
                MinScale = 0.0;
            }
            res[0] = MinScale.ToString();
            res[1] = MaxScale.ToString();
        }
        catch
        {
            res[0] = "0";
            res[1] = "1000000000";
        }
        return res;
    }

    public bool RemoveLineLayerRule(int ind, out bool isEmptyFilter)
    {
        isEmptyFilter = false;
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode del_node = null;
            XmlNodeList nodes = this.LayerDefXml.GetElementsByTagName("LineRule");
            if (nodes.Count == 1)
            {
                XmlNode ruleNode = this.LayerDefXml.GetElementsByTagName("LineRule")[0];
                ruleNode.SelectSingleNode("Filter").InnerText = "";
                isEmptyFilter = true;
            }
            else
            {
                for (int i = 0; i < nodes.Count; i++)
                {
                    if (nodes[i].SelectSingleNode("Filter").InnerText.Split("=".ToCharArray())[1].ToString() == ind.ToString())
                    {
                        del_node = nodes[i];
                    }
                }
                if (del_node != null)
                {
                    this.LayerDefXml.GetElementsByTagName("LineTypeStyle")[0].RemoveChild(del_node);
                }
            }
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool RemovePointLayerRule(int ind, out bool isEmptyFilter)
    {
        isEmptyFilter = false;
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode del_node = null;
            XmlNodeList nodes = this.LayerDefXml.GetElementsByTagName("PointRule");
            if (nodes.Count == 1)
            {
                XmlNode ruleNode = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
                ruleNode.SelectSingleNode("Filter").InnerText = "";
                isEmptyFilter = true;
            }
            else
            {
                for (int i = 0; i < nodes.Count; i++)
                {
                    if (nodes[i].SelectSingleNode("Filter").InnerText.Split("=".ToCharArray())[1].ToString() == ind.ToString())
                    {
                        del_node = nodes[i];
                    }
                }
                if (del_node != null)
                {
                    this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(del_node);
                }
            }
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool RemovePolygonLayerRule(int ind, out bool isEmptyFilter)
    {
        isEmptyFilter = false;
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode del_node = null;
            XmlNodeList nodes = this.LayerDefXml.GetElementsByTagName("AreaRule");
            if (nodes.Count == 1)
            {
                XmlNode ruleNode = this.LayerDefXml.GetElementsByTagName("AreaRule")[0];
                ruleNode.SelectSingleNode("Filter").InnerText = "";
                isEmptyFilter = true;
            }
            else
            {
                for (int i = 0; i < nodes.Count; i++)
                {
                    if (nodes[i].SelectSingleNode("Filter").InnerText.Split("=".ToCharArray())[1].ToString() == ind.ToString())
                    {
                        del_node = nodes[i];
                    }
                }
                if (del_node != null)
                {
                    this.LayerDefXml.GetElementsByTagName("AreaTypeStyle")[0].RemoveChild(del_node);
                }
            }
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool RemoveTextLayerRule(int ind, out bool isEmptyFilter)
    {
        isEmptyFilter = false;
        try
        {
            if (this.LayerDefXmlString != "")
            {
                this.LayerDefXml = new XmlDataDocument();
                this.LayerDefXml.LoadXml(this.LayerDefXmlString);
            }
            else if (this.LayerDefXml == null)
            {
                return false;
            }
            XmlNode del_node = null;
            XmlNodeList nodes = this.LayerDefXml.GetElementsByTagName("PointRule");
            if (nodes.Count == 1)
            {
                XmlNode ruleNode = this.LayerDefXml.GetElementsByTagName("PointRule")[0];
                ruleNode.SelectSingleNode("Filter").InnerText = "";
                isEmptyFilter = true;
            }
            else
            {
                for (int i = 0; i < nodes.Count; i++)
                {
                    if (nodes[i].SelectSingleNode("Filter").InnerText.Split("=".ToCharArray())[1].ToString() == ind.ToString())
                    {
                        del_node = nodes[i];
                    }
                }
                if (del_node != null)
                {
                    this.LayerDefXml.GetElementsByTagName("PointTypeStyle")[0].RemoveChild(del_node);
                }
            }
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public string ReplaceHebrewForXml(string sSrc)
    {
        string result = "";
        foreach (char s in sSrc)
        {
            result = result + string.Format("&#x{0:x3};", (int) s);
        }
        return result;
    }

    public bool SelectedFeatures(string sContent)
    {
        try
        {
            this.LayerDefXml = new XmlDataDocument();
            this.LayerDefXml.PreserveWhitespace = true;
            this.LayerDefXml.LoadXml(sContent);
            this.LayerDefXmlString = sContent;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool UpdateFilterLayerDef(string filter)
    {
        try
        {
            int i;
            if ((this.LayerDefXml.GetElementsByTagName("Filter").Count > 0) && (filter != ""))
            {
                this.LayerDefXml.GetElementsByTagName("Filter")[0].InnerText = filter;
            }
            XmlNodeList propsList = this.LayerDefXml.GetElementsByTagName("PropertyMapping");
            for (i = 0; i < propsList.Count; i++)
            {
                XmlNodeList childNodes = propsList[i].ChildNodes;
                childNodes.Item(3).InnerText = this.ReplaceHebrewForXml(childNodes.Item(3).InnerText);
            }
            XmlNodeList pnlst = this.LayerDefXml.GetElementsByTagName("LegendLabel");
            for (i = 0; i < pnlst.Count; i++)
            {
                pnlst.Item(i).InnerText = this.ReplaceHebrewForXml(pnlst.Item(i).InnerText);
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool UpdateLayerDef(string datasource, string featurename, string tooltip)
    {
        try
        {
            if (datasource != "")
            {
                this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            }
            if (featurename != "")
            {
                this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            }
            if ((this.LayerDefXml.GetElementsByTagName("ToolTip").Count > 0) && (tooltip != ""))
            {
                this.LayerDefXml.GetElementsByTagName("ToolTip")[0].InnerText = tooltip;
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool UpdateLayerDef(string datasource, string featurename, string tooltip, string filter)
    {
        try
        {
            if (datasource != "")
            {
                this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            }
            if (featurename != "")
            {
                this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            }
            if ((this.LayerDefXml.GetElementsByTagName("ToolTip").Count > 0) && (tooltip != ""))
            {
                this.LayerDefXml.GetElementsByTagName("ToolTip")[0].InnerText = tooltip;
            }
            if ((this.LayerDefXml.GetElementsByTagName("Filter").Count > 0) && (filter != ""))
            {
                this.LayerDefXml.GetElementsByTagName("Filter")[0].InnerText = filter;
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool UpdateLayerHatraotDef(string datasource, string featurename, string tooltip)
    {
        try
        {
            this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            if (featurename != "")
            {
                this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            }
            if (this.LayerDefXml.GetElementsByTagName("ToolTip").Count > 0)
            {
                this.LayerDefXml.GetElementsByTagName("ToolTip")[0].InnerText = tooltip;
            }
            XmlNodeList pnlst = this.LayerDefXml.GetElementsByTagName("LegendLabel");
            for (int i = 0; i < pnlst.Count; i++)
            {
                pnlst.Item(i).InnerText = this.ReplaceHebrewForXml(pnlst.Item(i).InnerText);
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool UpdatePredictLayerDef(string datasource, string featurename, string tooltip, string sPredict)
    {
        try
        {
            if (datasource != "")
            {
                this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            }
            if (featurename != "")
            {
                this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            }
            if ((this.LayerDefXml.GetElementsByTagName("ToolTip").Count > 0) && (tooltip != ""))
            {
                this.LayerDefXml.GetElementsByTagName("ToolTip")[0].InnerText = tooltip;
            }
            string[] Arr = sPredict.Split(",".ToCharArray());
            XmlNodeList pnlst = this.LayerDefXml.GetElementsByTagName("LegendLabel");
            for (int i = 0; i < pnlst.Count; i++)
            {
                pnlst.Item(i).InnerText = Arr[i];
                if (Arr.Length < (i + 2))
                {
                    break;
                }
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool UpdateSignFilterLayerDef(string filter)
    {
        try
        {
            int i;
            if ((this.LayerDefXml.GetElementsByTagName("Filter").Count > 0) && (filter != ""))
            {
                this.LayerDefXml.GetElementsByTagName("Filter")[0].InnerText = filter;
            }
            XmlNodeList propsList = this.LayerDefXml.GetElementsByTagName("PropertyMapping");
            for (i = 0; i < propsList.Count; i++)
            {
                XmlNodeList childNodes = propsList[i].ChildNodes;
                childNodes.Item(3).InnerText = this.ReplaceHebrewForXml(childNodes.Item(3).InnerText);
            }
            XmlNodeList pnlst = this.LayerDefXml.GetElementsByTagName("LegendLabel");
            for (i = 0; i < pnlst.Count; i++)
            {
                pnlst.Item(i).InnerText = this.ReplaceHebrewForXml(pnlst.Item(i).InnerText);
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public void UpdateTabaLayerDef(string QXml)
    {
        try
        {
            XmlDataDocument pXml = new XmlDataDocument();
            QXml = QXml.Replace("Join1", "Join");
            pXml.LoadXml(QXml);
            XmlNode pRule = pXml.GetElementsByTagName("VectorScaleRange")[0].Clone();
            this.LayerDefXml.GetElementsByTagName("VectorLayerDefinition")[0].AppendChild(this.LayerDefXml.ImportNode(pRule, true));
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
        }
        catch
        {
        }
    }

    public bool UpdateTabaLayerDef(string datasource, string featurename, string tooltip)
    {
        try
        {
            this.LayerDefXml.GetElementsByTagName("ResourceId")[0].InnerText = datasource;
            this.LayerDefXml.GetElementsByTagName("FeatureName")[0].InnerText = featurename;
            if (this.LayerDefXml.GetElementsByTagName("ToolTip").Count > 0)
            {
                this.LayerDefXml.GetElementsByTagName("ToolTip")[0].InnerText = tooltip;
            }
            XmlNodeList pnlst = this.LayerDefXml.GetElementsByTagName("LegendLabel");
            for (int i = 0; i < pnlst.Count; i++)
            {
                pnlst.Item(i).InnerText = this.ReplaceHebrewForXml(pnlst.Item(i).InnerText);
            }
            this.layerDefContent = new MgByteSource(Encoding.UTF8.GetBytes(this.LayerDefXml.OuterXml), this.LayerDefXml.OuterXml.Length).GetReader();
            this.LayerDefXmlString = this.LayerDefXml.OuterXml;
            return true;
        }
        catch
        {
            return false;
        }
    }
}

